﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace MvcApplication1.Models
{
    public class ManagerSearch<T>
    {
        protected T searchview;
        
        public T SearchView { get { return searchview; } }
    }

    public class DataViewManagerSearch : ManagerSearch<DataView>
    {
        public DataViewManagerSearch(DataView dv, string itemname, string itemSN, string addtime, string linkaddr)
        {
            string searchstring="";
            if (string.IsNullOrEmpty (itemname) != true) searchstring = "itemName='" + itemname+"'";
            if (string.IsNullOrEmpty(itemSN) != true) 
            {
                if (string.IsNullOrEmpty(searchstring) != true) searchstring += " AND ";
                searchstring += "itemSN='" + itemSN+"'";
            }
            if (string.IsNullOrEmpty(addtime) != true)
            {
                if (string.IsNullOrEmpty(searchstring) != true) searchstring += " AND ";
                searchstring+="addTime>='"+addtime+" 00:00:00' AND addTime<='"+addtime+" 23:59:00'";
            }
            if (string.IsNullOrEmpty(linkaddr) != true)
            {
                if (string.IsNullOrEmpty(searchstring) != true) searchstring += " AND ";
                searchstring+="linkAddr LIKE '*" + linkaddr+"*'";
            }
            if (string.IsNullOrEmpty(searchstring) != true)
            {
                searchview = new DataView(dv.Table);
                searchview.RowFilter = searchstring;
            }
            else
                searchview = dv;
        }
    }
    public class IQueryableBrandsManagerSearch : ManagerSearch<IQueryable<Brand>>
    {
        public IQueryableBrandsManagerSearch(IQueryable<Brand> dbset, string brandname)
        {
            if (string.IsNullOrEmpty(brandname) != true)
            {
                searchview = dbset.Where(b => b.BrandName == brandname);
            }
            else
                searchview = dbset;
        }
        //设置set是因为在排序时需要对searchview进行写操作
        public new IQueryable<Brand> SearchView { get { return searchview; } set { searchview = value; } }
    }

    public class IQueryableUsersSearch : ManagerSearch<IQueryable<User>>
    {
        public IQueryableUsersSearch(IQueryable<User> dbset, string username)
        {
            if (string.IsNullOrEmpty(username) != true)
            {
                searchview = dbset.Where(b => b.username == username);
            }
            else
                searchview = dbset;
        }
        public new IQueryable<User> SearchView { get { return searchview; } set { searchview = value; } }
    }
}